package cn.wangzg.course1201;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Time: 2020/5/7
 * Author: wangzhiguo
 * Description: 功能描述
 */
public class MyDraw extends View {

    private Paint mPaint;  //绘制线条的Path
    private Path mPath;      //记录用户绘制的Path
    private Canvas mCanvas;  //内存中创建的Canvas
    private Bitmap mBitmap;  //缓存绘制的内容

    private int mLastX;
    private int mLastY;

    public MyDraw(Context context) {
        super(context);
        init();
    }

    public MyDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     *
     * @param context 上下文 调用方法
     * @param attrs 允许自定义属性，处理
     * @param defStyleAttr 默认样式属性
     */
    public MyDraw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPath = new Path();
        mPaint = new Paint();   //初始化画笔
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);//反锯齿
        mPaint.setDither(true);
        //mPaint.setStyle(Paint.Style.STROKE);

        //mPaint.setStrokeWidth(20);   // 设置画笔宽度
    }
    //测量,
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        // 初始化bitmap,Canvas
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //创建画布
        mCanvas = new Canvas(mBitmap);
    }

    //重写该方法，在这里绘图
    @Override
    protected void onDraw(Canvas canvas) {
        Rect rect = new Rect(50,0,150,50);
        canvas.translate(200, 200);
        for(int i = 0; i < 6;i++){
            canvas.rotate(60);
            canvas.drawRect(rect, mPaint);
        }
    }

    //绘制线条
    private void drawPath(Canvas canvas){
        //绘制自定义的画布上
        canvas.drawPath(mPath, mPaint);
    }


}
